package es.ucm.fdi.isbc.viviendas.funciones_similitud;


import es.ucm.fdi.isbc.viviendas.representacion.ExtrasFinca;
import jcolibri.exception.NoApplicableSimilarityFunctionException;
import jcolibri.method.retrieve.NNretrieval.similarity.LocalSimilarityFunction;


public class SimExtrasFinca implements LocalSimilarityFunction {
	
	
	@Override
	public double compute(Object caseObject, Object queryObject)
			throws NoApplicableSimilarityFunctionException {
		//Si los dos objetos son null se devuelve 0
		if ((caseObject == null) || (queryObject == null))
			return 0;
		if (!(caseObject instanceof ExtrasFinca))
			throw new jcolibri.exception.NoApplicableSimilarityFunctionException(this.getClass(), caseObject.getClass());
		if (!(queryObject instanceof ExtrasFinca))
			throw new jcolibri.exception.NoApplicableSimilarityFunctionException(this.getClass(), queryObject.getClass());

		return (simExtras((ExtrasFinca)caseObject,(ExtrasFinca)queryObject));
		
	}


	private double simExtras(ExtrasFinca caso, ExtrasFinca query) {
		double sim=0.0;
		double sumSim=4.0;
		//Calculamos la similitud sumando con distinto peso
		if(caso.isAscensor()==query.isAscensor()) sim = sim + 0.8;
		if(caso.isEnergiaSolar()==query.isEnergiaSolar()) sim = sim + 0.5;
		if(caso.isGarajePrivado()==query.isGarajePrivado()) sim = sim + 0.7;
		if(caso.isParkingComunitario()==query.isParkingComunitario()) sim = sim + 0.6;
		if(caso.isServPorteria()==query.isServPorteria()) sim = sim + 0.3;
		if(caso.isTrastero()==query.isTrastero()) sim = sim + 0.8;
		if(caso.isVideoportero()==query.isVideoportero()) sim = sim + 0.3;
		//Dividimos la suma por el total
		return (double)(sim/sumSim);
	}


	@Override
	public boolean isApplicable(Object caseObject, Object queryObject) {
		//Comprobamos que los objetos son ExtrasFinca o null
		if((caseObject==null)&&(queryObject==null))
			return true;
		else if(caseObject==null)
			return queryObject instanceof ExtrasFinca;
		else if(queryObject==null)
			return caseObject instanceof  ExtrasFinca;
		else
			return (caseObject instanceof  ExtrasFinca)&&(queryObject instanceof  ExtrasFinca);

	}

}

